let app = getApp()

Page({
  data: {
    userInfo: app.globalData.userInfo,
    hidden: true,
    imgSrc: '/images/mine/mine@2x.png',
    title: '修改昵称',
    placeholder: '给自己取个响亮的名字',
    asyn: 0,
    version: 'text',
    max: 24,
    min: 0,
    defaultData: null,
    thisMsg: '昵称需满足4-24字符',
    byteLen: 0,
    defaultStr: '',
    colorStyle: 'color: #A4A4A4'
  },
  onLoad: function (options) {

  },
  claerInput: function () {
    // console.log('clear')
    this.setData({
      defaultData: ''
    })
  },
  changeSrc: function () {
    let that = this
    let source = "album"
    wx.showActionSheet({
      itemList: ['相册', '默认头像库', '拍照'],
      success: function (res) {
        if (res.tapIndex == 1){
          // console.log(res.tapIndex)
          wx.navigateTo({
            url: '/pages/mine/photoStore/photo'
          })
          return;
        }
        if (res.tapIndex == 0) {
          source = "album"
        } else {
          source = "camera"
        }
        wx.chooseImage({
          count: 1,
          sizeType: ['compressed'],
          sourceType: [source],
          success: function (res) {
            let tempFilePaths = res.tempFiles[0]
            if (tempFilePaths.size > (2 * 1024 * 1024)) {
              wx.showModal({
                content: '请上传2M内的图片文件',
                confirmColor: '#2A8EFF',
                showCancel: false,
                confirmText: '知道了'
              })
            } else {
              wx.showLoading({
                title: '正在保存',
                mask: true
              })
              wx.uploadFile({
                url: app.globalData.baseUrl + '/file/upload',
                filePath: tempFilePaths.path,
                name: 'file',
                success: function (res) {
                  // console.log(res)
                  let data = JSON.parse(res.data)
                  if (data.url) {
                    that.updateUserInfo({
                      "photo": data.url
                    }, "头像修改成功")
                  }
                }
              })
            }
          }
        })
      },
      fail: function (res) {
        // console.log(res.errMsg)
      }
    })
  },
  changeNick: function () {
    this.setData({
      title: '修改昵称',
      placeholder: '给自己取个响亮的名字',
      thisMsg: '昵称需满足4-24字符',
      hidden: false,
      asyn: 0,
      version: 'text',
      max: 24,
      defaultData: this.data.userInfo.nickname,
      colorStyle: 'color: #A4A4A4'
    })
  },
  changeQ: function () {
    this.setData({
      title: '修改QQ号',
      placeholder: '请输入QQ号',
      thisMsg: '只能为数字，长度：5-18位',
      hidden: false,
      asyn: 1,
      version: 'number',
      min: 5,
      max: 18,
      defaultData: this.data.userInfo.QQ,
      colorStyle: 'color: #A4A4A4'
    })
  },
  modal_click_Hidden: function () {
    this.setData({
      hidden: true,
    })
  },
  formSubmit: function (e) {
    let _this = this
    let asyn = this.data.asyn

    switch (asyn) {
      case 0:
        if (e.detail.value.data) {
          if (e.detail.value.data == this.data.userInfo.nickname) {
            wx.showToast({
              title: '昵称尚未修改',
              image: '/images/register/cuowu@2x.png'
            })
            return
          } else {
            if (this.data.byteLen < 4) {
              this.setData({
                colorStyle: 'color: #FF6252'
              })
              wx.showToast({
                title: '昵称格式错误',
                image: '/images/register/cuowu@2x.png'
              })
              return
            }
            wx.showLoading({
              title: '正在保存',
              mask: true
            })
            wx.request({
              url: app.globalData.baseUrl + '/checkNickName',
              data: {
                cluesOrStuId: wx.getStorageSync("userInfo").cluesOrStuId,
                nickName: e.detail.value.data
              },
              method: 'post',
              header: {
                'content-type': 'application/json'
              },
              success: function (res) {
                if (res.data.code == "0") {
                  _this.updateUserInfo({
                    'nickname': e.detail.value.data
                  }, "昵称修改成功")
                } else {
                  wx.hideLoading()
                  wx.showToast({
                    title: '该昵称已被使用',
                    image: '/images/register/cuowu@2x.png'
                  })
                }

              }
            })
          }
        } else {
          wx.showToast({
            title: '请输入昵称',
            image: '/images/register/cuowu@2x.png'
          })
          return
        }
        break;
      case 1:
        if (e.detail.value.data) {
          // console.log(e.detail)
          if (e.detail.value.data.length < this.data.min) {
            wx.showToast({
              title: '超出有效范围',
              image: '/images/register/cuowu@2x.png'
            });
            return;
          }
          wx.showLoading({
            title: '正在保存',
            mask: true
          })
          this.updateUserInfo({
            'QQ': e.detail.value.data
          }, "QQ修改成功")
        } else {
          wx.showToast({
            title: '请输入QQ号',
            image: '/images/register/cuowu@2x.png'
          })
          return
        }
        break;
    }
    this.setData({
      hidden: true
    })
  },
  formReset: function () {
    this.setData({
      hidden: true,
    })
  },
  updateUserInfo: function (opts, title) {
    let _this = this
    let getUserInfo = wx.getStorageSync("userInfo")

    wx.request({
      url: app.globalData.baseUrl + '/WXupdateInfo',
      data: {
        cluesOrStuId: getUserInfo.cluesOrStuId,
        type: getUserInfo.type,
        info: opts
      },
      method: 'post',
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
        wx.hideLoading()
        if (res.data.code == 0) {
          wx.showToast({
            title: title,
            image: '/images/register/chenggong@2x.png'
          })
          _this.setData({
            userInfo: res.data.data.info
          })
          Object.assign(app.globalData.userInfo, res.data.data.info)
        } else {
          wx.showToast({
            title: '保存失败',
            image: '/images/register/cuowu@2x.png'
          })
        }
      }
    })
  },
  dataSolve: function (e) {
    let strData = ''
    let dataStr = e.detail.value
    if (this.data.asyn == 0) {
      let byteLen = dataStr.replace(/[^\x00-\xff]/gi, "--").length
      let strLen = dataStr.length
      if (byteLen >= 25) {
        switch (this.data.byteLen) {
          case 24:
            strData = this.data.defaultStr
            break;
          case 0:
            strData = ''
            break;
          default:
            strData = this.data.defaultStr
        }
      } else {
        strData = dataStr
      }
      byteLen = strData.replace(/[^\x00-\xff]/gi, "--").length > 4 ? 4 : strData.replace(/[^\x00-\xff]/gi, "--").length
      this.setData({
        byteLen: byteLen,
        defaultStr: strData
      })
      return strData
    }
    else if (this.data.asyn == 1) {
      this.setData({
        defaultData: e.detail.value.replace(/[^\d]/g, '')
      })
    }
  },
  onShow: function () {
    this.setData({
      userInfo: app.globalData.userInfo
    })
  }
})